//
// File: xiaoboband.cpp
//
// MATLAB Coder version            : 3.4
// C/C++ source code generated on  : 16-Nov-2021 20:58:22
//

// Include Files
#include "rt_nonfinite.h"
#include "xiaoboband.h"
#include "xiaoboband_emxutil.h"
#include "idwt.h"
#include "thselect.h"
#include "dwt.h"

// Function Definitions

//
// Arguments    : const double x[128]
//                emxArray_real_T *y
// Return Type  : void
//
void xiaoboband(const double x[128], emxArray_real_T *y)
{
  emxArray_real_T *c;
  int i0;
  int xv_size[2];
  unsigned char l[5];
  double xv_data[128];
  emxArray_real_T *d;
  int k;
  double b_xv_data[68];
  int b_xv_size[2];
  double d_data[68];
  int d_size[2];
  int loop_ub;
  double thrs[3];
  int tmp;
  int first[3];
  emxArray_real_T *unusedU0;
  int last[3];
  double thr;
  emxArray_real_T *acol;
  double b_tmp;
  double b_x;
  emxArray_real_T *b_unusedU0;
  emxArray_real_T *b_acol;
  emxArray_int32_T *r0;
  int p;
  unsigned char b_l[6];
  double b_thrs[4];
  int b_first[4];
  int b_last[4];
  emxArray_real_T *c_acol;
  emxInit_real_T(&c, 2);
  i0 = c->size[0] * c->size[1];
  c->size[0] = 1;
  c->size[1] = 0;
  emxEnsureCapacity_real_T(c, i0);
  for (i0 = 0; i0 < 5; i0++) {
    l[i0] = 0;
  }

  xv_size[0] = 1;
  xv_size[1] = 128;
  memcpy(&xv_data[0], &x[0], sizeof(double) << 7);
  l[4] = 128;
  emxInit_real_T(&d, 2);
  for (k = 0; k < 3; k++) {
    dwt(xv_data, xv_size, b_xv_data, b_xv_size, d_data, d_size);
    xv_size[0] = 1;
    xv_size[1] = b_xv_size[1];
    loop_ub = b_xv_size[0] * b_xv_size[1];
    if (0 <= loop_ub - 1) {
      memcpy(&xv_data[0], &b_xv_data[0], (unsigned int)(loop_ub * (int)sizeof
              (double)));
    }

    i0 = d->size[0] * d->size[1];
    d->size[0] = 1;
    d->size[1] = d_size[1] + c->size[1];
    emxEnsureCapacity_real_T(d, i0);
    loop_ub = d_size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      d->data[d->size[0] * i0] = d_data[d_size[0] * i0];
    }

    loop_ub = c->size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      d->data[d->size[0] * (i0 + d_size[1])] = c->data[c->size[0] * i0];
    }

    i0 = c->size[0] * c->size[1];
    c->size[0] = 1;
    c->size[1] = d->size[1];
    emxEnsureCapacity_real_T(c, i0);
    loop_ub = d->size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      c->data[c->size[0] * i0] = d->data[d->size[0] * i0];
    }

    l[3 - k] = (unsigned char)d_size[1];
  }

  i0 = d->size[0] * d->size[1];
  d->size[0] = 1;
  d->size[1] = xv_size[1] + c->size[1];
  emxEnsureCapacity_real_T(d, i0);
  loop_ub = xv_size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    d->data[d->size[0] * i0] = xv_data[i0];
  }

  loop_ub = c->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    d->data[d->size[0] * (i0 + xv_size[1])] = c->data[c->size[0] * i0];
  }

  i0 = c->size[0] * c->size[1];
  c->size[0] = 1;
  c->size[1] = d->size[1];
  emxEnsureCapacity_real_T(c, i0);
  loop_ub = d->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    c->data[c->size[0] * i0] = d->data[d->size[0] * i0];
  }

  l[0] = (unsigned char)xv_size[1];
  for (i0 = 0; i0 < 3; i0++) {
    thrs[i0] = l[i0];
  }

  for (k = 0; k < 2; k++) {
    thrs[k + 1] += thrs[k];
  }

  for (i0 = 0; i0 < 3; i0++) {
    first[i0] = (int)thrs[i0] + 1;
  }

  tmp = first[0];
  first[0] = first[2];
  first[2] = tmp;
  for (i0 = 0; i0 < 3; i0++) {
    last[i0] = (l[3 - i0] + first[i0]) - 1;
  }

  emxInit_real_T(&unusedU0, 2);
  i0 = unusedU0->size[0] * unusedU0->size[1];
  unusedU0->size[0] = 1;
  unusedU0->size[1] = c->size[1];
  emxEnsureCapacity_real_T(unusedU0, i0);
  loop_ub = c->size[0] * c->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    unusedU0->data[i0] = c->data[i0];
  }

  for (i0 = 0; i0 < 3; i0++) {
    thrs[i0] = 0.0;
  }

  thr = thselect(c);
  for (k = 0; k < 3; k++) {
    thrs[k] = thr;
    for (tmp = first[k] - 1; tmp + 1 <= last[k]; tmp++) {
      b_tmp = std::abs(c->data[tmp]) - thrs[k];
      b_x = c->data[tmp];
      if (c->data[tmp] < 0.0) {
        b_x = -1.0;
      } else if (c->data[tmp] > 0.0) {
        b_x = 1.0;
      } else {
        if (c->data[tmp] == 0.0) {
          b_x = 0.0;
        }
      }

      unusedU0->data[tmp] = b_x * ((b_tmp + std::abs(b_tmp)) / 2.0);
    }
  }

  emxInit_real_T1(&acol, 1);
  i0 = acol->size[0];
  acol->size[0] = (unsigned char)xv_size[1];
  emxEnsureCapacity_real_T1(acol, i0);
  for (k = 0; k < l[0]; k++) {
    acol->data[k] = unusedU0->data[k];
  }

  emxInit_real_T1(&b_unusedU0, 1);
  emxInit_real_T1(&b_acol, 1);
  emxInit_int32_T(&r0, 1);
  for (p = 2; p >= 0; p--) {
    for (i0 = 0; i0 < 3; i0++) {
      first[i0] = 0;
      last[i0] = 0;
    }

    first[2] = l[0] + 1;
    last[2] = l[0] + l[1];
    for (tmp = 1; tmp >= 0; tmp--) {
      first[tmp] = first[tmp + 1] + l[2 - tmp];
      last[tmp] = (first[tmp] + l[3 - tmp]) - 1;
    }

    if (first[p] > last[p]) {
      i0 = 1;
      tmp = 0;
    } else {
      i0 = first[p];
      tmp = last[p];
    }

    k = r0->size[0];
    r0->size[0] = (tmp - i0) + 1;
    emxEnsureCapacity_int32_T(r0, k);
    loop_ub = tmp - i0;
    for (k = 0; k <= loop_ub; k++) {
      r0->data[k] = i0 + k;
    }

    k = b_unusedU0->size[0];
    b_unusedU0->size[0] = (tmp - i0) + 1;
    emxEnsureCapacity_real_T1(b_unusedU0, k);
    loop_ub = tmp - i0;
    for (i0 = 0; i0 <= loop_ub; i0++) {
      b_unusedU0->data[i0] = unusedU0->data[r0->data[i0] - 1];
    }

    i0 = b_acol->size[0];
    b_acol->size[0] = acol->size[0];
    emxEnsureCapacity_real_T1(b_acol, i0);
    loop_ub = acol->size[0];
    for (i0 = 0; i0 < loop_ub; i0++) {
      b_acol->data[i0] = acol->data[i0];
    }

    idwt(b_acol, b_unusedU0, (double)l[4 - p], acol);
  }

  i0 = c->size[0] * c->size[1];
  c->size[0] = 1;
  c->size[1] = 0;
  emxEnsureCapacity_real_T(c, i0);
  for (i0 = 0; i0 < 6; i0++) {
    b_l[i0] = 0;
  }

  xv_size[0] = 1;
  xv_size[1] = 128;
  memcpy(&xv_data[0], &x[0], sizeof(double) << 7);
  b_l[5] = 128;
  for (k = 0; k < 4; k++) {
    dwt(xv_data, xv_size, b_xv_data, b_xv_size, d_data, d_size);
    xv_size[0] = 1;
    xv_size[1] = b_xv_size[1];
    loop_ub = b_xv_size[0] * b_xv_size[1];
    if (0 <= loop_ub - 1) {
      memcpy(&xv_data[0], &b_xv_data[0], (unsigned int)(loop_ub * (int)sizeof
              (double)));
    }

    i0 = d->size[0] * d->size[1];
    d->size[0] = 1;
    d->size[1] = d_size[1] + c->size[1];
    emxEnsureCapacity_real_T(d, i0);
    loop_ub = d_size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      d->data[d->size[0] * i0] = d_data[d_size[0] * i0];
    }

    loop_ub = c->size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      d->data[d->size[0] * (i0 + d_size[1])] = c->data[c->size[0] * i0];
    }

    i0 = c->size[0] * c->size[1];
    c->size[0] = 1;
    c->size[1] = d->size[1];
    emxEnsureCapacity_real_T(c, i0);
    loop_ub = d->size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      c->data[c->size[0] * i0] = d->data[d->size[0] * i0];
    }

    b_l[4 - k] = (unsigned char)d_size[1];
  }

  i0 = d->size[0] * d->size[1];
  d->size[0] = 1;
  d->size[1] = xv_size[1] + c->size[1];
  emxEnsureCapacity_real_T(d, i0);
  loop_ub = xv_size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    d->data[d->size[0] * i0] = xv_data[i0];
  }

  loop_ub = c->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    d->data[d->size[0] * (i0 + xv_size[1])] = c->data[c->size[0] * i0];
  }

  i0 = c->size[0] * c->size[1];
  c->size[0] = 1;
  c->size[1] = d->size[1];
  emxEnsureCapacity_real_T(c, i0);
  loop_ub = d->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    c->data[c->size[0] * i0] = d->data[d->size[0] * i0];
  }

  emxFree_real_T(&d);
  b_l[0] = (unsigned char)xv_size[1];
  for (i0 = 0; i0 < 4; i0++) {
    b_thrs[i0] = b_l[i0];
  }

  for (k = 0; k < 3; k++) {
    b_thrs[k + 1] += b_thrs[k];
  }

  for (i0 = 0; i0 < 4; i0++) {
    b_first[i0] = (int)b_thrs[i0] + 1;
  }

  for (k = 0; k < 2; k++) {
    tmp = b_first[k];
    b_first[k] = b_first[3 - k];
    b_first[3 - k] = tmp;
  }

  for (i0 = 0; i0 < 4; i0++) {
    b_last[i0] = (b_l[4 - i0] + b_first[i0]) - 1;
  }

  i0 = unusedU0->size[0] * unusedU0->size[1];
  unusedU0->size[0] = 1;
  unusedU0->size[1] = c->size[1];
  emxEnsureCapacity_real_T(unusedU0, i0);
  loop_ub = c->size[0] * c->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    unusedU0->data[i0] = c->data[i0];
  }

  for (i0 = 0; i0 < 4; i0++) {
    b_thrs[i0] = 0.0;
  }

  thr = thselect(c);
  for (k = 0; k < 4; k++) {
    b_thrs[k] = thr;
    for (tmp = b_first[k] - 1; tmp + 1 <= b_last[k]; tmp++) {
      b_tmp = std::abs(c->data[tmp]) - b_thrs[k];
      b_x = c->data[tmp];
      if (c->data[tmp] < 0.0) {
        b_x = -1.0;
      } else if (c->data[tmp] > 0.0) {
        b_x = 1.0;
      } else {
        if (c->data[tmp] == 0.0) {
          b_x = 0.0;
        }
      }

      unusedU0->data[tmp] = b_x * ((b_tmp + std::abs(b_tmp)) / 2.0);
    }
  }

  emxFree_real_T(&c);
  i0 = b_acol->size[0];
  b_acol->size[0] = (unsigned char)xv_size[1];
  emxEnsureCapacity_real_T1(b_acol, i0);
  for (k = 0; k < b_l[0]; k++) {
    b_acol->data[k] = unusedU0->data[k];
  }

  emxInit_real_T1(&c_acol, 1);
  for (p = 3; p >= 0; p--) {
    for (i0 = 0; i0 < 4; i0++) {
      b_first[i0] = 0;
      b_last[i0] = 0;
    }

    b_first[3] = b_l[0] + 1;
    b_last[3] = b_l[0] + b_l[1];
    for (tmp = 2; tmp >= 0; tmp--) {
      b_first[tmp] = b_first[tmp + 1] + b_l[3 - tmp];
      b_last[tmp] = (b_first[tmp] + b_l[4 - tmp]) - 1;
    }

    if (b_first[p] > b_last[p]) {
      i0 = 1;
      tmp = 0;
    } else {
      i0 = b_first[p];
      tmp = b_last[p];
    }

    k = r0->size[0];
    r0->size[0] = (tmp - i0) + 1;
    emxEnsureCapacity_int32_T(r0, k);
    loop_ub = tmp - i0;
    for (k = 0; k <= loop_ub; k++) {
      r0->data[k] = i0 + k;
    }

    k = b_unusedU0->size[0];
    b_unusedU0->size[0] = (tmp - i0) + 1;
    emxEnsureCapacity_real_T1(b_unusedU0, k);
    loop_ub = tmp - i0;
    for (i0 = 0; i0 <= loop_ub; i0++) {
      b_unusedU0->data[i0] = unusedU0->data[r0->data[i0] - 1];
    }

    i0 = c_acol->size[0];
    c_acol->size[0] = b_acol->size[0];
    emxEnsureCapacity_real_T1(c_acol, i0);
    loop_ub = b_acol->size[0];
    for (i0 = 0; i0 < loop_ub; i0++) {
      c_acol->data[i0] = b_acol->data[i0];
    }

    idwt(c_acol, b_unusedU0, (double)b_l[5 - p], b_acol);
  }

  emxFree_int32_T(&r0);
  emxFree_real_T(&c_acol);
  emxFree_real_T(&b_unusedU0);
  emxFree_real_T(&unusedU0);
  i0 = y->size[0] * y->size[1];
  y->size[0] = 1;
  y->size[1] = acol->size[0];
  emxEnsureCapacity_real_T(y, i0);
  loop_ub = acol->size[0];
  for (i0 = 0; i0 < loop_ub; i0++) {
    y->data[y->size[0] * i0] = acol->data[i0] - b_acol->data[i0];
  }

  emxFree_real_T(&b_acol);
  emxFree_real_T(&acol);
}

//
// File trailer for xiaoboband.cpp
//
// [EOF]
//
